package com.example;

public class HouseRobberIi {

    public int rob(int[] nums) {
        int length = nums.length;
        if(length == 1){
            return nums[length -1];
        }else if(length == 2){
            return Math.max(nums[length -1], nums[length -2]);
        }
        return Math.max(rob(nums, 0, length -2), rob(nums, 1, length - 1));
    }

    private int rob(int[] nums, int start, int end){
        int[] dp = new int[nums.length];
        dp[start] = nums[start];
        dp[start + 1] = Math.max(nums[start], nums[start + 1]);
        for (int i = start + 2; i <=end; i++) {
            dp[i] = Math.max(nums[i] + dp[i -2], dp[i - 1]);
        }
        return dp[end];
    }

    public static void main(String[] args) {
        HouseRobberIi houseRobberIi = new HouseRobberIi();
        houseRobberIi.rob(new int[]{2,3,2});
    }
}
